#include "loginpage.h"
#include "ui_loginpage.h"
#include "verify.h"
#include "networkcontroller.h"
#include <QLineEdit>
#include <QTextCodec>
#include <QDebug>
#include <QRegularExpression>
#include <QRegularExpressionMatch>
#include <QMessageBox>
#include "register.h"




LoginPage::LoginPage(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::LoginPage)
{
    ui->setupUi(this);
    NetWorkController::getInstance()->m_connetToServer();
    QTextCodec::setCodecForLocale(QTextCodec::codecForName("UTF-8"));
    connect(NetWorkController::getInstance()->getCSocket(),&QTcpSocket::disconnected,[](){
        NetWorkController::getInstance()->setIsConnect(false);
    });

}

LoginPage::~LoginPage()
{
    delete ui;
}

bool LoginPage::tryConnect()
{
    if(!NetWorkController::getInstance()->getIsConnect())
    {
        NetWorkController::getInstance()->m_connetToServer();
    }
    if(!NetWorkController::getInstance()->getIsConnect())
    {
        QMessageBox::warning(nullptr,"错误","无法连接至服务器");
        return false;
    }
    return true;
}

void LoginPage::on_btn_eye_pressed()
{
    ui->pw_le->setEchoMode(QLineEdit::Normal);
}


void LoginPage::on_btn_eye_released()
{
    ui->pw_le->setEchoMode(QLineEdit::Password);

}


void LoginPage::on_register_btn_clicked()
{
    r = new Register(this);
    this->hide();
    r->exec();
    this->show();

}


void LoginPage::on_login_btn_clicked()
{
    if(!tryConnect())
    {
        return;
    }
    v = new Verify(this);
    v->exec();
    if(!v->getIsVerified())
    {
        return;
    }
    QString username = ui->un_le->text();
    QString password = Register::toMd5(ui->pw_le->text());

    if(username == "")
    {
        QMessageBox::warning(this,tr("错误"), tr("请输入用户名"));
        return;
    }

    if(password == "")
    {
        QMessageBox::warning(this, tr("错误！"), tr("请输入密码"));
        return;
    }


    // 验证用户名是否符合要求
    QRegularExpression emailRegex("^\\b[A-Za-z0-9._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}\\b$");
    QRegularExpressionMatch emailMatch = emailRegex.match(username);

    if (!emailMatch.hasMatch())
    {

        QMessageBox::warning(this, "错误！", "用户名格式错误！");
        return;
    }

    // 验证密码是否符合要求
    QRegularExpression passwordRegex("(--|#|%|&|;|\\*|\\+|,|-|/|:|<|>|=|\\?|\\[|\\]|\\^|\\{|\\}|\\|)");
    QRegularExpressionMatch passwordMatch = passwordRegex.match(password);
    if (passwordMatch.hasMatch())
    {

        QMessageBox::warning(this, "错误！", "密码格式错误！");
        return;
    }

    NetWorkController::getInstance()->m_sendLoginMessage(username,password);

}


void LoginPage::on_un_le_textChanged(const QString &arg1)
{
    if(arg1 == "")
    {
        ui->lbl_usnm->setText("UserName Is Null");
        ui->lbl_usnm->setStyleSheet("color:red;");
    }
    else
    {
        ui->lbl_usnm->setText("");
    }
}


void LoginPage::on_pw_le_textChanged(const QString &arg1)
{
    if(arg1 == "")
    {
        ui->lbl_psw->setText("PassWord Is Null");
        ui->lbl_psw->setStyleSheet("color:red;");
    }
    else
    {
        ui->lbl_psw->setText("");
    }
}



